//
//  YUYANetWorking.m
//  com.yigu.yuya
//
//  Created by hxp on 16/3/4.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYINetWorking.h"
#import "AppDelegate.h"


@implementation YAYINetWorking
static BOOL isFirst = NO;
static BOOL canCHeckNetwork = NO;


+(void)postJsonSerilizeWithURL:(NSString *)url param:(NSDictionary *)param finish:(void (^)(NSData *, NSDictionary *, NSError *,NSDictionary *))cb{
    if (!isFirst) {
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            canCHeckNetwork = YES;
        }];
        isFirst = YES;
    }
    BOOL isOK = [[AFNetworkReachabilityManager sharedManager] isReachable];
    if(isOK == NO && canCHeckNetwork == YES){
        NSError *error = [NSError errorWithDomain:@"网络错位" code:100 userInfo:nil];
        cb(nil, nil,error,nil);
        return;
    }
    
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    
    AFHTTPSessionManager *manager = [app sharedHTTPSession];
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [[NSSet  alloc] initWithObjects:@"application/json", nil];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",API,url];
    [manager POST:urlStr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        cb(responseObject,responseObject,nil,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] options:NSJSONReadingMutableContainers error:nil];
        cb(nil,nil,error,dict);
    }];
    
}


+ (void)getWithURL:(NSString *)url param:(NSDictionary *)dict finish:(void (^)(NSData *, NSDictionary *, NSError *))cb{
    if (!isFirst) {
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            canCHeckNetwork = YES;
        }];
        isFirst = YES;
    }
    BOOL isOK = [[AFNetworkReachabilityManager sharedManager] isReachable];
    if(isOK == NO && canCHeckNetwork == YES){
        NSError *error = [NSError errorWithDomain:@"网络错位" code:100 userInfo:nil];
        cb(nil, nil,error);
        return;
    }
    
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    AFHTTPSessionManager *manager = [app sharedHTTPSession];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",API,url];
    
    [manager GET:urlStr parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        cb(responseObject, responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        cb(nil,nil,error);
    }];
}


+(void)postWithURL:(NSString *)url param:(NSDictionary *)param finish:(void (^)(NSData *, NSDictionary *, NSError *))cb{
    if (!isFirst) {
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            canCHeckNetwork = YES;
        }];
        isFirst = YES;
    }
    BOOL isOK = [[AFNetworkReachabilityManager sharedManager] isReachable];
    if(isOK == NO && canCHeckNetwork == YES){
        NSError *error = [NSError errorWithDomain:@"网络错位" code:100 userInfo:nil];
        cb(nil, nil,error);
        return;
    }
    
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    AFHTTPSessionManager *manager = [app sharedHTTPSession];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",API,url];
    [manager POST:urlStr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         cb(responseObject,responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        cb(nil,nil,error);
    }];
    
}


+(void)postJsonSerilizeWithURL:(NSString *)url APIStr:(NSString *)APIStr param:(NSDictionary *)param finish:(void (^)(NSData *, NSDictionary *, NSError *,NSDictionary *))cb{
    
    if (!isFirst) {
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            canCHeckNetwork = YES;
        }];
        isFirst = YES;
    }
    BOOL isOK = [[AFNetworkReachabilityManager sharedManager] isReachable];
    if(isOK == NO && canCHeckNetwork == YES){
        NSError *error = [NSError errorWithDomain:@"网络错位" code:100 userInfo:nil];
        cb(nil, nil,error,nil);
        return;
    }
    
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    AFHTTPSessionManager *manager = [app sharedHTTPSession];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [[NSSet  alloc] initWithObjects:@"application/json", nil];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",APIStr, url];
    [manager POST:urlStr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        cb(responseObject,responseObject,nil,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        YAYILog(@"erro:%@", error);
        if (error.userInfo[@"com.alamofire.serialization.response.error.data"]) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] options:NSJSONReadingMutableContainers error:nil];
            cb(nil,nil,error,dict);
        }
    }];
    
}

@end
